package com.abe.jmcl.android.menu;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abe.jmcl.R;
import com.abe.jmcl.android.about.AppAboutActivity;
import com.abe.jmcl.android.menu.fragment.MenuFrameFragment;
import com.abe.jmcl.android.menu.fragment.MenuModuleFragment;
import com.abe.jmcl.utils.NormalUtils;
import com.abeframe.core.base.MVPBaseFragmentActivity;
import com.abeframe.core.base.MVPBasePresenter;

import butterknife.BindView;
import butterknife.OnClick;

public class MainMenuActivity extends MVPBaseFragmentActivity {

    @Override
    protected MVPBasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getThemeResId() {
        return 0;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_menu_main;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initFragment();
    }

    @Override
    protected void initDatas() {
        itemTextTitle.setText(NormalUtils.getString(R.string.app_name));
    }

    private void initFragment() {
        frameFragment = MenuFrameFragment.newInstance();
        moduleFragment = MenuModuleFragment.newInstance();
        mViewPager
                .setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
        mViewPager.setOnPageChangeListener(new PagerListener());
        mViewPager.setCurrentItem(0);
    }

    @OnClick(R.id.item_text_about)
    public void aboutClicked() {
        goToActivity(AppAboutActivity.class);
    }

    @OnClick(R.id.item_layout_module)
    public void moduleClicked() {
        if (mViewPager != null) {
            mViewPager.setCurrentItem(0);
        }
    }

    @OnClick(R.id.item_layout_frame)
    public void frameClicked() {
        if (mViewPager != null) {
            mViewPager.setCurrentItem(1);
        }
    }

    private class PagerListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int pos) {
        }

        @Override
        public void onPageScrolled(int pos, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int pos) {
            if (pos == 1) {
                itemImageModule.setBackgroundResource(R.drawable.icon_tabbar_component);
                itemImageFrame.setBackgroundResource(R.drawable.icon_tabbar_util_selected);
                itemTextModule.setTextColor(NormalUtils.getColorRes(R.color.theme_hint));
                itemTextFrame.setTextColor(NormalUtils.getColorRes(R.color.theme_color));
            } else {
                itemImageModule.setBackgroundResource(R.drawable.icon_tabbar_component_selected);
                itemImageFrame.setBackgroundResource(R.drawable.icon_tabbar_util);
                itemTextModule.setTextColor(NormalUtils.getColorRes(R.color.theme_color));
                itemTextFrame.setTextColor(NormalUtils.getColorRes(R.color.theme_hint));
            }
        }
    }

    private class ViewPagerAdapter extends FragmentPagerAdapter {

        ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int pos) {
            switch (pos) {
                case 0:
                    return moduleFragment;
                case 1:
                    return frameFragment;
            }
            throw new IllegalStateException("No fragment at position " + pos);
        }

        @Override
        public int getCount() {
            return 2;
        }
    }


    @BindView(R.id.item_text_title)
    TextView itemTextTitle;
    @BindView(R.id.item_viewpager)
    ViewPager mViewPager;
    @BindView(R.id.item_image_module)
    ImageView itemImageModule;
    @BindView(R.id.item_text_module)
    TextView itemTextModule;
    @BindView(R.id.item_layout_module)
    LinearLayout itemLayoutModule;
    @BindView(R.id.item_image_frame)
    ImageView itemImageFrame;
    @BindView(R.id.item_text_frame)
    TextView itemTextFrame;
    @BindView(R.id.item_layout_frame)
    LinearLayout itemLayoutFrame;

    private MenuFrameFragment frameFragment;
    private MenuModuleFragment moduleFragment;
}
